
#define NUM_SQRT_ENTRIES  65536 


#include <stdio.h>

unsigned int  get_sqrt_tab[NUM_SQRT_ENTRIES];

unsigned int sqrt(  pi_sqrd )
unsigned long   pi_sqrd;
{


   unsigned long    sq;
   unsigned int     pi_guess;
   unsigned int     delta_pi;

   if ( pi_sqrd == 0 ) return ( 0 );

   pi_guess = 0x8000;  
   delta_pi = 0x4000; 


   do
   {

	sq = pi_guess*pi_guess;

        /*printf ( "%10x %10ld %10ld %10x\n\n", pi_guess, sq, pi_sqrd, delta_pi );*/

	if ( pi_sqrd > sq ) pi_guess += delta_pi;
	else                pi_guess -= delta_pi;

        delta_pi = delta_pi / 2;

    }  while ( sq != pi_sqrd );

    return ( (unsigned int)pi_guess );
}

/*-------begin changed by wangwei-------------*/
//main ( )
//{
//   unsigned int    number;
//
//   printf ( "unsigned short  sqrt_table[NUM_SQRT_ENTRIES] = {\n" );
//
//   for ( number = 0 ; number <= 65536  ; number+=256 )
//   {
//     get_sqrt_tab[number] = sqrt ( number*0x1000000 );
//
//     if ((number%(8*256))==0)
//         printf ( "\n/*jdp=%9ld*/ ", (unsigned long)number*number );
//
//     if ( number == (NUM_SQRT_ENTRIES-1) ) printf ( "%7d ", get_sqrt_tab[number] );
//     else                                  printf ( "%7d,", get_sqrt_tab[number] );
//   }
//
//
//
//  printf ( "};\n");
//
//}

/*--------end changed------*/
